<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div >
        <!-- 搜索 -->
        <label class="el-form-item-label">媒体编号</label>
        <el-input v-model="query.uid" clearable placeholder="媒体编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <!-- <label class="el-form-item-label">媒体名称</label>
        <el-input v-model="query.uname" clearable placeholder="媒体名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        -->
        <label class="el-form-item-label">是否结算</label>
        <el-select v-model="query.statusdesc" clearable size="small" placeholder="是否结算" class="filter-item" style="width: 110px"  @change="crud.toQuery">
          <el-option v-for="item in dict.yn" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>

        <date-range-picker
          v-model="query.cdate"
          start-placeholder="订单开始时间"
          end-placeholder="订单结束时间"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->

      <el-button
        class="filter-item"
        size="mini"
        type="success"
        icon="el-icon-edit"
        v-permission="permission.add"
        :disabled="crud.selections.length < 1"
        @click="toSettle(crud.selections)"
      >
        设置为已结算
      </el-button>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="id" prop="id">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结算金额">
            <el-input v-model="form.rateamt" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结算笔数">
            <el-input v-model="form.ratecount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否结算">
            <el-select v-model="form.statusdesc" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.yn"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注1">
            <el-input v-model="form.extra1" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注2">
            <el-input v-model="form.extra2" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <comp v-if="crud.update">
        <label class="el-form-item-label" style="color: red;">
          订单总计：{{crud.totalData.dc}}笔（{{ _fenToYuan('','',crud.totalData.da) }}元）&#12288;&#12288;
          已结算：{{crud.totalData.sc}}笔（{{ _fenToYuan('','',crud.totalData.sa) }}元）&#12288;&#12288;
          待结算：{{ _fenToYuan('','',crud.totalData.dsa) }}元&#12288;&#12288;
        </label>
      </comp>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="cdate" label="日期" />
        <el-table-column prop="uid" label="媒体编号" />
        <el-table-column prop="uname" label="媒体名称" />
        <el-table-column prop="orderamt" label="订单总额"  :formatter="_fenToYuan" />
        <el-table-column prop="rateamt" label="结算金额"  :formatter="_fenToYuan" />
        <el-table-column prop="ratecount" label="结算笔数" />
        <el-table-column prop="statusdesc" label="是否结算">
          <template slot-scope="scope">
            {{ dict.label.yn[scope.row.statusdesc] }}
          </template>
        </el-table-column>
        <el-table-column prop="extra1" label="结算时间" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDaySheel from '@/api/rate/daySheel'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, cdate: null, rate: null, uid: null, uname: null, orderamt: null, rateamt: null, ratecount: null, statusdesc: null, extra1: null, extra2: null }
export default {
  name: 'DaySheel',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['yn'],
  cruds() {
    return CRUD({ title: 'daySheel', url: 'api/daySheel', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDaySheel }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'order:add'],
        edit: ['1admin1', 'daySheel:edit1'],
        del: ['11admin', 'daySheel:del1']
      },
      rules: {
        id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        cdate: [
          { required: true, message: '日期不能为空', trigger: 'blur' }
        ],
        rate: [
          { required: true, message: '返利不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'uid', display_name: '媒体编号' },
        { key: 'uname', display_name: '媒体名称' },
        { key: 'statusdesc', display_name: '是否结算' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    _fenToYuan(row, col, cellValue) {
      if (typeof cellValue === 'undefined' || cellValue === null || cellValue === '') {
        return '0'
      } else {
        var num = Number(cellValue)
        if (!num) {
          return num + '.00'
        } else {
          num = Math.round((num) * 100) / 10000
          num = num.toFixed(2)
          num += ''
          var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g
          return num.replace(reg, '$1,')
        }
      }
    },
    toSettle(datas) {
      this.$confirm(`确认已结算选中的${datas.length}条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        this.crud.doDelete(datas)
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
